﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;

namespace BTA.BLL
{
    public class Person : IBLLObject<Person>
    {
        private readonly DAL.Person _dalObj;
        private readonly DAL.BtaEntities _db;

        #region Constructors
        /// <summary>
        /// Creates a new person object
        /// </summary>
        public Person()
        {
            _dalObj = new DAL.Person();
            _db = new DAL.BtaEntities();
            _db.AddToperson(_dalObj);
            _dalObj.Created = DateTime.Now;
        }

        /// <summary>
        /// Returns an existing person of the database, based on its id
        /// </summary>
        /// <param name="id"></param>
        public Person(int id)
        {
            _db = new DAL.BtaEntities();
            _dalObj = _db.person.First(p => p.PersonId == id);
        }
        #endregion

        #region Properties
        /// <summary>
        /// The Id of the current Person
        /// </summary>
        public int Id { get { return _dalObj.PersonId; } }

        public string Name { get { return _dalObj.Name; } }

        public string EmailAddress { get { return _dalObj.EmailAddress; } }

        public string MobileNumber { get { return _dalObj.MobileNumber; } }

        public DateTime? Birthday { get { return _dalObj.Birthday; } }

        public double? Saldo { get { return _dalObj.Saldo; } }

        //public Photo Photo { get { return Mapper.Map<DAL.Photo, BLL.Photo>(_dalObj.photo); } }

        public bool? Deleted { get { return _dalObj.Deleted; } }

        /// <summary>
        /// The creation data of this Person
        /// </summary>
        public DateTime? Created { get { return _dalObj.Created; } }

        //public User CreatedBy {get{return _dalObj.person1}}

        public DateTime? Updated { get { return _dalObj.Updated; } }

        //public User UpdatedBy {get{return _dalObj.person2}}
        #endregion

        #region Statics

        public static List<Person> GetList()
        {
            var db = new DAL.BtaEntities();
            return Mapper.Map<List<DAL.Person>, List<Person>>(db.person.ToList());
        }

        #endregion

        #region Methods
        public void Save()
        {

        }

        public void Save(string name)
        {
            _dalObj.Name = name;
            _db.SaveChanges();
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
